<template>
	<el-dialog v-model="dialog" :title="form.id ? '修改租户套餐' : '添加租户套餐'" width="60%" :before-close="handleClose">
		<el-form ref="formRef" :model="form" label-width="120px" :rules="rules" status-icon>
			<el-form-item label="套餐名称" prop="name">
				<el-input v-model="form.name" show-word-limit maxlength="50"></el-input>
			</el-form-item>
			<el-form-item label="子标题" prop="subTitle">
				<el-input v-model="form.subTitle"></el-input>
			</el-form-item>
			<el-form-item label="原价（元）" prop="originalPrice">
				<el-input-number :min="0.01" :precision="2" v-model="form.originalPrice"></el-input-number>
			</el-form-item>
			<el-form-item label="销售价格（元）" prop="salesPrice">
				<el-input-number :min="0.01" :precision="2" v-model="form.salesPrice"></el-input-number>
			</el-form-item>
			<el-form-item label="应用系统" prop="appKeys">
				<el-select v-model="form.appKeys" multiple>
					<el-option v-for="item in applicationKeys" :key="item.dictValue" :label="item.dictLabel"
						:value="item.dictValue" />
				</el-select>
			</el-form-item>
			<el-form-item label="描述" prop="description">
				<Editor v-model:get-html="form.description" height="500px" :disable="false"></Editor>
			</el-form-item>
			<el-form-item label="状态" prop="status">
				<el-radio-group v-model="form.status">
					<el-radio label="0">停用</el-radio>
					<el-radio label="1">正常</el-radio>
				</el-radio-group>
			</el-form-item>
		</el-form>
		<template #footer>
			<span class="dialog-footer">
				<el-button @click="handleClose">关 闭</el-button>
				<el-button type="primary" :loading="loading" @click="submitForm(formRef)">确 认</el-button>
			</span>
		</template>
	</el-dialog>
</template>
<script lang="ts">
import { defineComponent, defineAsyncComponent, ref, reactive, toRefs } from 'vue';
import { addObj, editObj, getById } from '/@/api/upms/tenantpackage';
import { getList } from '/@/api/upms/dictvalue';
import { ElMessage, FormInstance, FormRules } from 'element-plus';
interface ApplicationKey {
	dictLabel: string;
	dictValue: string;
}
interface DataState {
	form: {
		id: string | undefined;
		status: string;
		name: string;
		subTitle: string;
		originalPrice: number;
		salesPrice: number;
		appKey: string;
		description: string;
		appKeys: Array<string>;
	};
	rules: any;
	applicationKeys: Array<ApplicationKey>;
}
export default defineComponent({
	components: { Editor: defineAsyncComponent(() => import('/@/components/editor/index.vue')) },
	emits: ['initPage'],
	setup(props, { emit }) {
		const data = reactive<DataState>({
			applicationKeys: [],
			form: { id: undefined, status: '', name: '', subTitle: '', originalPrice: 0, salesPrice: 0, appKey: '', description: '', appKeys: [] },
			rules: {
				name: [
					{
						required: true,
						message: '请输入套餐名称',
						trigger: 'change',
					},
				],
				originalPrice: [
					{
						required: true,
						message: '请输入原价（元）',
						trigger: 'change',
					},
				],
				salesPrice: [
					{
						required: true,
						message: '请输入销售价格（元）',
						trigger: 'change',
					},
				],
				appKeys: [
					{
						required: true,
						message: '请选择应用系统',
						trigger: 'change',
					},
				],
				status: [
					{
						required: true,
						message: '请选择状态',
						trigger: 'change',
					},
				],
			},
		});
		const dialog = ref(false);
		const loading = ref(false);
		const formRef = ref();
		const initForm = (row: any | undefined) => {
			dialog.value = true;
			getDictList();
			if (row && row.id) {
				getDetail(row.id);
			}
		};
		const getDetail = (id: string) => {
			loading.value = true;
			getById(id).then((response: any) => {
				data.form = response.data.data;
				data.form.appKeys = data.form.appKey.split(",");
				loading.value = false;
			});
		};
		/**
		 * 关闭事件
		 */
		const handleClose = () => {
			resetForm(formRef.value);
		};
		/**
		 * 重置表单
		 */
		const resetForm = (formEl: FormInstance | undefined) => {
			if (!formEl) return;
			data.form.id = undefined;
			loading.value = false;
			dialog.value = false;
			formEl.resetFields();
		};
		/**
		 * 提交按钮
		 */
		const submitForm = async (formEl: FormInstance | undefined) => {
			if (!formEl) return;
			await formEl.validate((valid, fields) => {
				if (valid) {
					loading.value = true;
					data.form.appKey = data.form.appKeys.join(",");
					if (data.form.id) {
						//修改
						edit();
					} else {
						//新增
						add();
					}
				}
			});
		};
		/**
		 * 新增
		 */
		const add = () => {
			addObj(data.form)
				.then((response: any) => {
					resetForm(formRef.value);
					ElMessage.success('新增成功');
					emit('initPage');
				})
				.catch(() => {
					loading.value = false;
				});
		};
		/**
		 * 修改
		 */
		const edit = () => {
			editObj(data.form)
				.then((response: any) => {
					resetForm(formRef.value);
					ElMessage.success('修改成功');
					emit('initPage');
				})
				.catch(() => {
					loading.value = false;
				});
		};
		const getDictList = () => {
			getList('application_key').then((response) => {
				data.applicationKeys = response.data.data;
			});
		};
		return {
			dialog,
			loading,
			formRef,
			initForm,
			submitForm,
			handleClose,
			...toRefs(data),
		};
	},
});
</script>
